package com.ha8.netease.views.team.profile
{
	import com.ha8.netease.App;
	import com.ha8.netease.PublicNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.models.vo.ViewModel;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 个人资料页面视图代理
	 * 
	 * @author rogo
	 */	
	public class ProfileMediator extends Mediator
	{
		public function ProfileMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'profileViewMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var model:ViewModel;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			view.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			view.friendLab.addEventListener(MouseEvent.CLICK, friend_clickHandler);
			view.FanLab.addEventListener(MouseEvent.CLICK, FanLab_clickHandler);
			view.FavLab.addEventListener(MouseEvent.CLICK, FavLab_clickHandler);
			view.MainLab.addEventListener(MouseEvent.CLICK, MainLab_clickHandler);
			
			displayUser();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():ProfileView
		{
			return viewComponent as ProfileView;
		}
		
		public function get proxy():WeiboProxy
		{
			return facade.retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
		
		private function displayUser():void
		{
			if (model &&
				model.equal(App.getInstance().profileModel))
				return;
			
			model = App.getInstance().profileModel;
			
			updateDisplay();
		}
		
		private function updateDisplay():void
		{
			view.title = model.user.name + '的资料';
			view.userImg.source = model.user.profile_image_url;
			view.userName.text = model.user.name;
			view.friendLab.text = '关注' + model.user.friends_count;
			view.FanLab.text = '粉丝' + model.user.followers_count;
			view.FavLab.text = '收藏' + model.user.favourites_count;
			view.MainLab.text = '微博' + model.user.statuses_count;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function addToStageHandler(e:Event):void
		{
			displayUser();	
		}
		
		private function friend_clickHandler(e:MouseEvent):void
		{
			var viewModel:ViewModel = new ViewModel();
			viewModel.user = model.user;
			viewModel.type = ViewModel.FRIENDS;
			sendNotification(PublicNotify.DISPLAY_USER_PAGE, viewModel);
		}
		
		private function FanLab_clickHandler(e:MouseEvent):void
		{
			var viewModel:ViewModel = new ViewModel();
			viewModel.user = model.user;
			viewModel.type = ViewModel.FANS;
			sendNotification(PublicNotify.DISPLAY_USER_PAGE, viewModel);
		}
		
		private function FavLab_clickHandler(e:MouseEvent):void
		{
			var viewModel:ViewModel = new ViewModel();
			viewModel.user = model.user;
			viewModel.type = ViewModel.FAVORITE;
			sendNotification(PublicNotify.DISPLAY_MAIN_PAGE, viewModel);
		}
		
		private function MainLab_clickHandler(e:MouseEvent):void
		{
			var viewModel:ViewModel = new ViewModel();
			viewModel.user = model.user;
			viewModel.type = ViewModel.MAIN;
			sendNotification(PublicNotify.DISPLAY_MAIN_PAGE, viewModel);
		}
		
	}
}